<template>
  <div class="liveSet">
    <div class="form-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>活动管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="table-bar">
      <el-button type="primary" size="medium" @click="handleAdd"
        >添加商品</el-button
      >
      <div class="inputSearch">
        <el-input
          placeholder="输入编号搜索"
          v-model="keyword"
          clearable
          @input="clearInput"
          @keyup.enter.native="searchInput"
        ></el-input>
        <el-button type="primary" size="medium" @click="searchInput"
          >搜索</el-button
        >
      </div>
    </div>
    <div class="partnerList_table">
      <div class="partnerList_bottom_table">
        <table>
          <tr>
            <th></th>
            <th>序号</th>
            <th>商品名称</th>
            <th>子标题</th>
            <th>商品类型</th>
            <th>商品主图</th>
            <!-- <th>单价</th> -->
            <th>库存</th>
            <th>排序</th>
            <!-- <th>添加时间</th> -->
            <th>状态</th>
            <th>操作</th>
          </tr>
          <tr v-for="(v, i) in tableList" :key="i">
            <td>
              <font
                class="checkbox"
                :class="{ check_active: checkBoxAll.includes(i) }"
                @click="handlerCheckbox(i)"
                ><i class="el-icon-check"></i
              ></font>
            </td>
            <td>{{ v.id }}</td>
            <td>{{ v.name }}</td>
            <td>{{ v.sub_name }}</td>
            <td>{{ v.type == 2 ? "虚拟商品" : "实物" }}</td>
            <td>
              <el-image
                v-if="v.img"
                style="width: 80px; height: 80px; margin-top: 20px"
                :src="v.img"
                :preview-src-list="[v.img]"
              >
              </el-image>
              <!-- <img style="max-width:200px;max-height:100px;" :src="v.img" alt=""/> -->
            </td>
            <!-- <td>{{v.price}}</td> -->
            <td>{{ v.stock }}</td>
            <td>{{ v.sort }}</td>
            <!-- <td>{{v.created_at}}</td> -->
            <td>{{ v.status == 1 ? "开启" : "关闭" }}</td>
            <td>
              <!-- <button class="editor" :class="{'delete':v.status==1}" @click="toChange(v.item_id,v.status)">{{v.status==1?'关闭':'开启'}}</button> -->
              <button class="yellow" @click="toDel(v.id)">删除</button>
              <button class="shelves" @click="toEdit(v.id)">编辑</button>
              <button class="yellow" @click="toQrcode(v.id)">二维码</button>
            </td>
          </tr>
          <tr v-if="tableList.length == 0">
            <td class="table_title" colspan="10">当前数据空空如也</td>
          </tr>
        </table>
        <div class="partnerList_bottom_page">
          <div class="partnerList_bottom_page_left">
            <div
              class="partnerList_bottom_page_left_checkAll"
              @click="handerCheckAll"
            >
              <font class="checkbox" :class="{ check_active: checkAll }"
                ><i class="el-icon-check"></i
              ></font>
            </div>
            全选
            <!-- <button class="partnerList_bottom_page_left_btn" @click="selectDel">删除</button> -->
          </div>
          <div class="partnerList_bottom_page_right">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="page"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              background
            >
            </el-pagination>
          </div>
        </div>
      </div>
    </div>

    <!-- 二维码弹出框 -->
    <el-dialog
      title="二维码"
      :visible.sync="dialogVisible"
      @closed="closedFn"
      width="25%"
    >
      <div style="width: 220px; height: 220px; margin: 0 auto">
        <img style="width: 220px; height: 220px" :src="qrImgUrl" alt="" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="donwloadQrImg">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      checkAll: false,
      page: 1,
      pageSize: 10,
      total: 0,
      checkBoxAll: [], // 选中的数据
      shelvesAlls: [],
      tableList: [],
      keyword: "",
      code: "",
      activity_id: "", //主活动id
      fatherName: "", //主活动名称

      dialogVisible: false,
      qrImgUrl: "", //二维码路径
    };
  },
  mounted() {
    this.getSetList();
  },
  methods: {
    // 获取活动商品列表o
    getSetList() {
      let params = {
        keyword: this.keyword,
        page: this.page,
        pagesize: this.pageSize,
      };
      this.$http.post("/api/root/activity/good/lists", params).then((res) => {
        console.log(res, "活动商品列表");
        if (res.code == 1) {
          this.tableList = res.data.data ? res.data.data : [];
          this.total = res.data.count;
        }
      });
    },

    // 节目列表
    toParty(id) {
      this.$router.push({ name: "partyList", query: { id } });
    },

    // 搜索
    searchInput() {
      if (this.keyword != "") {
        this.page = 1;
        this.pageSize = 10;
        this.getSetList();
      }
    },
    // 清空输入框
    clearInput() {
      if (this.keyword == "") {
        this.page = 1;
        this.pageSize = 10;
        this.getSetList();
      }
    },

    // 添加活动按钮
    handleAdd() {
      this.$router.push({ name: "activityGoodsAdd", query: {} });
    },

    // 删除按钮
    toDel(id) {
      this.$confirm("此操作将删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$http.post("/api/root/activity/good/del", { id }).then((res) => {
          console.log(res);
          if (res.code == 1) {
            this.getSetList();
            this.$message({
              type: "success",
              message: "删除成功",
            });
          } else {
            this.$message.warning(res.msg);
          }
        });
      });
    },

    // 编辑按钮
    toEdit(id) {
      this.$router.push({ name: "activityGoodsAdd", query: { id } });
    },

    // 更改状态
    toChange(id, sta) {
      let params = {
        id,
        status: sta == 1 ? 0 : 1,
      };
      this.$http.post("/api/root/activity/son/status", params).then((res) => {
        console.log(res, "更改状态");
        if (res.code == 1) {
          this.$message.success("更改成功");
          this.getSetList();
        } else {
          this.$message.warning(res.msg);
        }
      });
    },

    // 生成二维码按钮
    toQrcode(id) {
      this.$http.post("/api/root/activity/good/qrcode", { id }).then((res) => {
        console.log(res, "生成二维码");
        if (res.code == 1) {
          this.dialogVisible = true;
          this.qrImgUrl = res.data ? res.data : "";
        } else {
          this.$message.warning(res.msg);
        }
      });
    },

    // 关闭二维码对话框
    closedFn() {
      this.qrImgUrl = "";
    },

    // 保存二维码
    donwloadQrImg() {
      // console.log(this.qrImgUrl,'path');
      // let url = this.qrImgUrl.split('.com/')[1]
      // console.log(url,'000');
      this.dialogVisible = false;
      // var alink = document.createElement("a");
      // alink.href = `/api/${url}`;
      // alink.download = "pic"; //图片名
      // alink.click();

      // var a = document.createElement('a');
      //    a.setAttribute('href', this.qrImgUrl);
      //    a.setAttribute('target', '_blank');
      //    a.setAttribute('id', 'startTelMedicine');
      //    // 防止反复添加
      //    if(document.getElementById('startTelMedicine')) {
      //        document.body.removeChild(document.getElementById('startTelMedicine'));
      //    }
      //    document.body.appendChild(a);
      //    a.click();
      // let path = `/api/${url}`
      // console.log(path,'path1');
      // this.downloadIamge(this.qrImgUrl,'1111')
    },

    downloadIamge(imgsrc, name) {
      //下载图片地址和图片名
      // var image = new Image();
      // // 解决跨域 Canvas 污染问题
      // image.setAttribute("crossOrigin", "anonymous");
      // image.onload = function() {
      // var canvas = document.createElement("canvas");
      // canvas.width = image.width;
      // canvas.height = image.height;
      // var context = canvas.getContext("2d");
      // context.drawImage(image, 0, 0, image.width, image.height);
      // var url = canvas.toDataURL("image/png"); //得到图片的base64编码数据
      // var a = document.createElement("a"); // 生成一个a元素
      // var event = new MouseEvent("click"); // 创建一个单击事件
      // a.download = name || "photo"; // 设置图片名称
      // a.href = url; // 将生成的URL设置为a.href属性
      // a.dispatchEvent(event); // 触发a的单击事件
      // };
      // image.src = imgsrc;
    },

    // 多选
    handlerCheckbox(i) {
      console.log(i);
      let arr = this.checkBoxAll;
      if (arr.includes(i)) {
        let index = arr.indexOf(i);
        if (index != -1) {
          arr.splice(index, 1);
          this.shelvesAlls.splice(index, 1);
          if (this.checkBoxAll.length !== this.tableList.length) {
            this.checkAll = false;
          }
        }
      } else {
        this.checkBoxAll.push(i);
        this.shelvesAlls.push(this.tableList[i].id);
        if (this.checkBoxAll.length == this.tableList.length) {
          this.checkAll = true;
        }
      }
      console.log(this.shelvesAlls);
    },
    // 全选与全不选
    handerCheckAll() {
      this.checkAll = !this.checkAll;
      let arr = this.tableList.length;
      this.checkBoxAll = [];
      this.shelvesAlls = [];
      if (this.checkAll) {
        for (let i = 0; i < arr; i++) {
          this.checkBoxAll.push(i);
        }
        this.checkBoxAll.map((v) => {
          this.shelvesAlls.push(this.tableList[v].id.toString());
        });
      } else {
        this.checkBoxAll = [];
        this.shelvesAlls = [];
      }
      console.log(this.shelvesAlls);
    },

    //更改直播配置
    // toChange(id,sta){
    //     let status = sta==0?1:0;
    //     this.$http.post('/api/root/SystemConfig/changeStatus',{id,status}).then(res=>{
    //         console.log(res,'直播配置结果');
    //         if(res.code==1){
    //             this.$message.success('操作成功')
    //             this.getSetList()
    //         }else{
    //             this.$message.warning(res.msg)
    //         }
    //     })
    // },

    handleSizeChange(val) {
      this.page = 1;
      this.pageSize = val;
      this.getSetList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getSetList();
    },
  },
};
</script>
<style lang="less" scoped>
@import "index.less";
</style>